package com.xoms.front.constants;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 * 字符、字符数组处理
 */
public class StringConstants {

	private static StringConstants instance;
	private Map<String, String> strMaps;
	private Map<String, String[]> strArrayMaps;

	private StringConstants() {
		if (strMaps == null)
			strMaps = new HashMap<String, String>();

		if (strArrayMaps == null)
			strArrayMaps = new HashMap<String, String[]>();

		parserXml();

		init();
	}

	private void init() {
		WECHAT_APPID = strMaps.get("wechat_appid");
		WECHAT_APPSECRET = strMaps.get("wechat_appsecret");
		WECHAT_URL = strMaps.get("wechat_url");
		WECHAT_TOKEN = strMaps.get("wechat_token");
		WECHAT_ENCODING_AES_KEY = strMaps.get("wechat_encoding_aes_key");
		WECHAT_MCH_ID = strMaps.get("wechat_mch_id");
		WECHAT_MCH_KEY = strMaps.get("wechat_mch_key");
		WECHAT_OAUTH_SCOPE = strMaps.get("wechat_oauth_scope");
		WECHAT_CERT_PATH = strMaps.get("wechat_cert_path");
		WECHAT_CERTIFICATE_PATH = strMaps.get("wechat_certificate_path");
		WECHAT_PAY_CALLBACK = strMaps.get("wechat_pay_callback");

		ALIPAY_ACCOUNT = strMaps.get("alipay_account");
		ALIPAY_GATE_WAY = strMaps.get("alipay_gate_way");
		ALIPAY_PUBLIC_KEY = strMaps.get("alipay_public_key");
		ALIPAY_APP_ID = strMaps.get("alipay_app_id");
		ALIPAY_APP_PUBLIC_KEY = strMaps.get("alipay_app_public_key");
		ALIPAY_APP_PRIVATE_KEY = strMaps.get("alipay_app_private_key");
		ALIPAY_API_CONTENT_AES_KEY = strMaps.get("alipay_api_content_aes_key");
		ALIPAY_SIGN_TYPE = strMaps.get("alipay_sign_type");
		ALIPAY_CHARSET = strMaps.get("alipay_charset");
		ALIPAY_CALLBACK = strMaps.get("alipay_callback");

		ALIPAY_SANDBOX_ACCOUNT = strMaps.get("alipay_sandbox_account");
		ALIPAY_SANDBOX_GATE_WAY = strMaps.get("alipay_sandbox_gate_way");
		ALIPAY_SANDBOX_PUBLIC_KEY = strMaps.get("alipay_sandbox_public_key");
		ALIPAY_SANDBOX_APP_ID = strMaps.get("alipay_sandbox_app_id");
		ALIPAY_SANDBOX_APP_PUBLIC_KEY = strMaps.get("alipay_sandbox_app_public_key");
		ALIPAY_SANDBOX_APP_PRIVATE_KEY = strMaps.get("alipay_sandbox_app_private_key");
		ALIPAY_SANDBOX_API_CONTENT_AES_KEY = strMaps.get("alipay_sandbox_api_content_aes_key");
		ALIPAY_SANDBOX_SIGN_TYPE = strMaps.get("alipay_sandbox_sign_type");
		ALIPAY_SANDBOX_CHARSET = strMaps.get("alipay_sandbox_charset");
		ALIPAY_SANDBOX_CALLBACK = strMaps.get("print_service_url");
	}

	public static StringConstants getInstance() {
		synchronized (StringConstants.class) {
			if (instance == null)
				instance = new StringConstants();
			return instance;
		}
	}

	/**
	 * 解析
	 */
	private void parserXml() {
		SAXBuilder builder = new SAXBuilder();
		try {
			Document doc = builder.build(this.getClass().getResourceAsStream("/assets/strings.xml"));
			Element rootEl = doc.getRootElement();
			// 获得所有string元素
			List<Element> results = rootEl.getChildren("string");
			for (Element result : results) {
				// 获取name、value
				String name = result.getAttributeValue("name");
				String value = result.getText();

				strMaps.put(name, value);
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取string value
	 * 
	 * @param name
	 * @return
	 */
	public String getString(String name) {
		return strMaps.get(name);
	}

	/**
	 * 带格式化，获取字符串
	 * 
	 * @param name
	 * @return
	 */
	public String getString(String name, Object... args) {
		String format = getString(name);
		if (format == null)
			return null;
		return String.format(format, args);
	}

	/**
	 * 获取字符数组
	 * 
	 * @param name
	 * @return
	 */
	public String[] getArray(String name) {
		return strArrayMaps.get(name);
	}

	public String WECHAT_APPID;
	public String WECHAT_APPSECRET;
	public String WECHAT_URL;
	public String WECHAT_TOKEN;
	public String WECHAT_ENCODING_AES_KEY;
	public String WECHAT_MCH_ID;
	public String WECHAT_MCH_KEY;
	public String WECHAT_OAUTH_SCOPE;
	public String WECHAT_CERT_PATH;
	public String WECHAT_CERTIFICATE_PATH;
	public String WECHAT_PAY_CALLBACK;

	public String ALIPAY_ACCOUNT;
	public String ALIPAY_GATE_WAY;
	public String ALIPAY_PUBLIC_KEY;
	public String ALIPAY_APP_ID;
	public String ALIPAY_APP_PUBLIC_KEY;
	public String ALIPAY_APP_PRIVATE_KEY;
	public String ALIPAY_API_CONTENT_AES_KEY;
	public String ALIPAY_SIGN_TYPE;
	public String ALIPAY_CHARSET;
	public String ALIPAY_CALLBACK;

	public String ALIPAY_SANDBOX_ACCOUNT;
	public String ALIPAY_SANDBOX_GATE_WAY;
	public String ALIPAY_SANDBOX_PUBLIC_KEY;
	public String ALIPAY_SANDBOX_APP_ID;
	public String ALIPAY_SANDBOX_APP_PUBLIC_KEY;
	public String ALIPAY_SANDBOX_APP_PRIVATE_KEY;
	public String ALIPAY_SANDBOX_API_CONTENT_AES_KEY;
	public String ALIPAY_SANDBOX_SIGN_TYPE;
	public String ALIPAY_SANDBOX_CHARSET;
	public String ALIPAY_SANDBOX_CALLBACK;
	
	public String PRINT_SERVICE_URL;

}
